﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizEvlutionImages : CustBizObject
    {
        private CDmDjkEvlutionImages m_CDmDjkEvlutionImages = new CDmDjkEvlutionImages();
        private CDmDjkEvlutionImages oDmDjkEvlutionImages
        {
            get { return m_CDmDjkEvlutionImages; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizEvlutionImages()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkEvlutionImages.DJK_EVLUTION_IMAGES);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["EVLUTION_IMAGES_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加 评论图片，每条评论的 图片不能超过5张
        /// </summary>
        /// <param name="evlutionID">评论编号</param>
        /// <param name="imagesName">图片名称</param>
        /// <param name="imagesSource">源评论图片</param>
        /// <param name="imageDefault">是否设为封面 默认是false</param>
        /// <returns></returns>
        public string AddEvlutionImages(string evlutionID, string imagesName, string imagesSource, bool imageDefault = false)
        {
            if (string.IsNullOrEmpty(evlutionID))
            {
                return "评论编号不能为空";
            }
            if (string.IsNullOrEmpty(imagesName))
            {
                return "图片名称不能为空";
            }
            if (string.IsNullOrEmpty(imagesSource))
            {
                return "源评论图片不能为空";
            }

            DataSet ds = new CDmDjkEvlutionImages();
            DataRow drow = ds.Tables[0].NewRow();
            drow["EVLUTION_IMAGES_ID"] = this.DataObj.NewID();
            drow["EVLUTION_ID"] = evlutionID;
            drow["EVLUTION_IMAGES_NAME"] = imagesName;
            drow["EVLUTION_IMAGES_SOURCE"] = imagesSource;
            drow["EVLUTION_IMAGES_DEFAULT"] = imageDefault ? 1 : 0;
            drow["IMAGES_IS_DELETED"] = 0;
            drow["IMAGES_IS_DISPLAY"] = 1;
            ds.Tables[0].Rows.Add(drow);

            this.Update(ds);
            return "OK";
        }
        /// <summary>
        /// 获取每条评论的 图片
        /// </summary>
        /// <param name="evlutionID"></param>
        /// <returns></returns>
        public DataSet GetEvlutionImages(string evlutionID)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("EvlutionID", ParameterDirection.Input, EDbType.String, 36, evlutionID));
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT EVLUTION_IMAGES_ID,EVLUTION_ID,EVLUTION_IMAGES_NAME,
            EVLUTION_IMAGES_SOURCE,EVLUTION_IMAGES_DEFAULT,IMAGES_IS_DELETED,IMAGES_IS_DISPLAY,IMAGES_RMK FROM [dbo].[DJK_EVLUTION_IMAGES] WHERE EVLUTION_ID=@EvlutionID", parmCollection);
            return ds;
        }


    }
}
